package com.rf.richfitwheel.admin.sys.controller;

import com.rf.richfitwheel.admin.annotation.UserOperateLog;
import com.rf.richfitwheel.admin.contants.UrlConstant;
import com.rf.richfitwheel.admin.sys.model.SysCityInfo;
import com.rf.richfitwheel.admin.sys.model.SysCurInfo;
import com.rf.richfitwheel.admin.sys.service.SysCityInfoService;
import com.rf.richfitwheel.admin.sys.service.SysCurInfoService;
import com.rf.richfitwheel.common.contants.CommonServiceError;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jia XiaoLong
 * @className SysCityInfoController
 * @Date 2022/1/17
 * @Description
 */
@RestController
@RequestMapping(UrlConstant.WEB_ADMIN_URL + "/cityInfo")
public class SysCityInfoController {
    private static Logger log = LoggerFactory.getLogger(SysCityInfoController.class);
    @Autowired
    private SysCityInfoService sysCityInfoService;


    /**
     * 所有配置列表
     */
    @GetMapping("/queryPageList")
    public R queryPageList(String page, String limit, String dataForm) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        params.put("limit", limit);
        params.put("dataForm", dataForm);
        PageUtils pages = sysCityInfoService.queryPage(params);
        return R.ok().put("page", pages);
    }

    /**
     * 所有配置列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map params) {
        List<SysCurInfo> list = sysCityInfoService.getCityList(params);
        return R.ok().put("list", list);
    }

    /**
     * 配置信息
     *
     * @throws Exception
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) throws Exception {
        SysCityInfo sysCityInfo = sysCityInfoService.selectById(id);
        return R.ok().put("sysCityInfo", sysCityInfo);
    }

    /**
     * 保存配置
     */
    @UserOperateLog(module = "系统功能", methods = "save", description = "新增国家信息")
    @PostMapping("/save")
    public R save(@RequestBody SysCityInfo sysCityInfo) {
        try {
            sysCityInfoService.save(sysCityInfo);
            return R.ok();
        } catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_SAVE.getMsg() + "国家地区" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_SAVE.getMsg() + "国家地区" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 修改配置
     */
    @UserOperateLog(module = "系统功能", methods = "update", description = "修改国家信息")
    @PostMapping("/update")
    public R update(@RequestBody SysCityInfo sysCityInfo) {

        try {
            sysCityInfoService.update(sysCityInfo);
            return R.ok();
        } catch (BusinessException e){
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "国家地区" + CommonServiceError.SERVICE_FAILURE.getMsg(), e);
            return R.error(CommonServiceError.SERVICE_MODIFY.getMsg() + "国家地区" + CommonServiceError.SERVICE_FAILURE.getMsg());
        }
    }

    /**
     * 删除配置
     */
    @UserOperateLog(module = "系统功能", methods = "delete", description = "删除国家信息")
    @PostMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        sysCityInfoService.deleteBatch(ids);

        return R.ok();
    }
}
